home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / pop2cli.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-25  |  3.7 KB  |  161 lines

  1. /* POP2 Client routines -- Not recommended.
  2.  *    Jan 92    William Allen Simpson
  3.  *        complete re-write to match new mailreader commands
  4.  *
  5.  *    partly based on a NNTP client design by Anders Klemets, SM0RGV
  6.  *    Originally authored by Mike Stockett (WA7DYX).
  7.  *
  8.  * History:
  9.  *    Modified 12 May 1991 by Mark Edwards (WA6SMN) to use new timer
  10.  *    facilities in NOS0423.  Fixed type mismatches spotted by C++.
  11.  *    Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  12.  *      with later releases (NOS0522).
  13.  *    Added into NOS by PA0GRI (and linted into "standard" C)
  14.  *     Modified 14 June 1987 by P. Karn for symbolic target addresses,
  15.  *      also rebuilt locking mechanism
  16.  *
  17.  *    Some code culled from previous releases of SMTP.
  18.  *    See that code for applicable copyright notices.
  19.  */
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include "global.h"
  23. #include "timer.h"
  24. #include "proc.h"
  25. #include "netuser.h"
  26. #include "socket.h"
  27. #include "cmdparse.h"
  28. #include "files.h"
  29. #include "mailcli.h"
  30. #include "mailutil.h"
  31. #include "smtp.h"
  32.  
  33. extern char Badhost[];
  34.  
  35. /* Response string keys */
  36. static char *greeting_rsp  = "+ POP2 ";
  37.  
  38. void
  39. pop2_job(unused,v1,p2)
  40. int unused;
  41. void *v1;
  42. void *p2;
  43. {
  44.     struct mailservers *np = v1;
  45.     struct sockaddr_in fsocket;
  46.     char buf[TLINELEN];
  47.     char *cp;
  48.     FILE *wfp = NULLFILE;
  49.     int s = -1;
  50.     int folder_len;
  51.     int msg_num = 0;
  52.  
  53.     if ( mailbusy( np ) )
  54.         return;
  55.  
  56.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  57.         /* No IP address found */
  58.         if (Mailtrace >= 1)
  59.             log(-1,"POP2 can't resolve host '%s'", np->hostname);
  60.         start_timer(&np->timer);
  61.         return;
  62.     }
  63.  
  64.     fsocket.sin_family = AF_INET;
  65.     fsocket.sin_port = IPPORT_POP2;
  66.  
  67.     s = socket(AF_INET,SOCK_STREAM,0);
  68.     sockmode(s,SOCK_ASCII);
  69.  
  70.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  71.         cp = sockerr(s);
  72.         if (Mailtrace >= 2)
  73.             log(s,"POP2 Connect failed: %s",
  74.                 cp != NULLCHAR ? cp : "");
  75.         goto quit;
  76.     }
  77.  
  78.     log(s,"POP2 Connected to mailhost %s", np->hostname);
  79.  
  80.     if ( mailresponse( s, buf, "banner" ) == -1)
  81.         goto quit;
  82.  
  83.     if (strncmp(buf,greeting_rsp,strlen(greeting_rsp)) != 0)
  84.         goto quitcmd;
  85.  
  86.     (void)usprintf(s,"HELO %s %s\n",np->username,np->password);
  87.  
  88.     if ( mailresponse( s, buf, "HELO" ) == -1)
  89.         goto quit;
  90.  
  91.     if (buf[0] != '#'
  92.       || (folder_len = atoi(&(buf[1]))) == 0) {
  93.         /* If there is no mail (the only time we get a "+"
  94.          * response back at this stage of the game),
  95.          * then just close out the connection, because
  96.          * there is nothing more to do!! */
  97.         goto quitcmd;
  98.     }
  99.  
  100.     if ((wfp = tmpfile()) == NULLFILE) {
  101.         if ( Mailtrace >= 1 )
  102.             log(s,"POP2 Cannot create %s", "tmp file" );
  103.         goto quitcmd;
  104.     }
  105.  
  106.     (void)usprintf(s,"READ\n");
  107.  
  108.     /* now, get the text */
  109.     while(TRUE) {
  110.         long msg_len;
  111.  
  112.         if ( mailresponse( s, buf, "read loop" ) == -1)
  113.             goto quit;
  114.  
  115.         if (buf[0] == '='
  116.          && (msg_len = atol(&(buf[1]))) > 0) {
  117.             (void)usprintf(s,"RETR\n");
  118.  
  119.             while ( msg_len > 0 ) {
  120.                 if (recvline(s,buf,TLINELEN) == -1)
  121.                     goto quit;
  122.  
  123.                 rip(buf);
  124.                 fprintf(wfp,"%s\n",buf);
  125.  
  126.                 msg_len -= (long)(strlen(buf)+2);/* Add CRLF */
  127.             }
  128.             (void)usprintf(s,"ACKD\n");
  129.  
  130.             msg_num++;
  131.         } else {
  132.             break;
  133.         }
  134.     }
  135.  
  136.     if ( folder_len > 0 ) {
  137.         /* testing the result is pointless,
  138.          * since POP2 already deleted mail.
  139.          */
  140.         copymail(buf, TLINELEN, wfp);
  141.  
  142.         tprintf("New mail arrived for %s from mailhost %s%c\n",
  143.                 np->mailbox,
  144.                 np->hostname,
  145.                 Mailquiet ? ' ' : '\007');
  146.         smtptick(NULL);     /* wake SMTP to send that mail */
  147.     }
  148.  
  149. quitcmd:
  150.     (void)usprintf(s,"QUIT\n");
  151.  
  152. quit:
  153.     log(s,"POP2 daemon exiting");
  154.     (void) close_s(s);
  155.  
  156.     if (wfp != NULLFILE)
  157.         fclose(wfp);
  158.     start_timer(&np->timer);
  159. }
  160.  
  161.